home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 13902 / 13902.xpi / content / overlay.js < prev    next >
Text File  |  2010-02-14  |  10KB  |  244 lines

  1. /* See license.txt for terms of usage */ 
  2.  
  3. /**
  4. *   CDblClicker 
  5. *   ----------------------------------------
  6. *   @Object for controlling double-clicks...
  7. *   @bYO!
  8. */ 
  9.  
  10. window.addEventListener("load", function(e) { CDblClicker.onLoad(e); }, false); 
  11.  
  12. /**
  13.  * @constructor
  14.  */
  15.  
  16. const CDblClicker = new function(){    
  17.                         // macro replacement...
  18.     const _bDebug      = false;   
  19.     function TRACE(s)
  20.     {
  21.         if (_bDebug)    
  22.             dump("####### " + s + " #######\n");
  23.     };
  24.                          
  25.     const _spClickDelay     = 250;   
  26.     var m_bInterClick       = false;  
  27.     var m_eClicked          = null;
  28.    
  29.     return {
  30.  
  31.         onLoad: function (aEvent) 
  32.         {               // initialization code           gBrowser.onTabBarDblClick = this.onTabBarDblClick;
  33.             gBrowser.mTabContainer.addEventListener("dblclick", this.onTabBarDblClick, true);
  34.             window.addEventListener("click",this.onHandleClick,true);  
  35.             window.addEventListener("mouseup",this.onHandleMouseup,true);  
  36.             this.initialized = true;                
  37.         },
  38.  
  39.         onTabBarDblClick: function (aEvent) 
  40.         {
  41.             //TRACE("onTabBarDblClick: - in " + aEvent.target.localName );            
  42.             if (aEvent.target.localName!="tab" || aEvent.button!=0 || aEvent.ctrlKey || aEvent.shiftKey || aEvent.altKey || aEvent.metaKey)
  43.                 return;
  44.             var nAction = gBrowser.mPrefs.getIntPref("extensions.dblclicker.dblclicktab")
  45.             switch ( nAction )
  46.             {
  47.                 case 1: var tTab = gBrowser.duplicateTab(gBrowser.selectedTab); 
  48.                         gBrowser.selectedTab = tTab;
  49.                         killEvent(aEvent);
  50.                         break;
  51.                 case 2: gBrowser.removeCurrentTab();
  52.                         killEvent(aEvent);
  53.             } 
  54.         },
  55.         
  56.         onHandleMouseup: function (aEvent)
  57.         {
  58.             //TRACE("onHandleMouseup: - in " + aEvent.detail );            
  59.             if (m_bInterClick || aEvent.button!=0 || aEvent.ctrlKey || aEvent.shiftKey || aEvent.altKey || aEvent.metaKey)
  60.             {
  61.                 //TRACE("onHandleMouseup: !pure - out");
  62.                 m_bInterClick = false;
  63.                 m_eClicked = null;                                          
  64.                 return;
  65.             }   
  66.             var target = aEvent.originalTarget;
  67.             if (target.localName != "menuitem" || !target.node || !PlacesUtils.nodeIsURI(target.node))
  68.                 return;
  69.                 
  70.             if (aEvent.detail==1)
  71.             {
  72.                 m_eClicked = aEvent.target;           
  73.                 killEvent(aEvent);        
  74.                 setTimeout(function(){ doMouseup(target, aEvent);}, _spClickDelay);        
  75.                 return;                       
  76.             }
  77.             if (aEvent.detail==2)
  78.             {
  79.                 killEvent(aEvent);                    
  80.                 var windowUtils = window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
  81. //                windowUtils.sendMouseEvent("mousedown", aEvent.clientX, aEvent.clientY, 0, 1, Ci.nsIDOMNSEvent.CONTROL_MASK); 
  82.                 windowUtils.sendMouseEvent("mouseup", aEvent.clientX, aEvent.clientY, 0, 1, Ci.nsIDOMNSEvent.CONTROL_MASK); 
  83.                 return;                       
  84.             }        
  85.             //TRACE("onHandleMouseup: - out " + aEvent.detail );                    
  86.         },
  87.                                
  88.         onHandleClick: function (aEvent)
  89.         {
  90.             //TRACE("onHandleClick: - in " + aEvent.detail );            
  91.             if (m_bInterClick || aEvent.button!=0 || aEvent.ctrlKey || aEvent.shiftKey || aEvent.altKey || aEvent.metaKey)
  92.             {
  93.                 //TRACE("onHandleClick: !pure - out");
  94.                 m_bInterClick = false;
  95.                 m_eClicked = null;                                          
  96.                 return;
  97.             }   
  98.             if (aEvent.target instanceof HTMLElement)
  99.             {
  100.                 if (aEvent.detail==1)
  101.                     onClickDoc(aEvent); 
  102.                 else  
  103.                     if (aEvent.detail==2)
  104.                         onDblClick(aEvent, false);   
  105.                 return;
  106.             }
  107.             if (aEvent.originalTarget.localName == "treechildren")
  108.             {
  109.                 if (aEvent.detail==1)
  110.                     onClickTree(aEvent);
  111.                 else
  112.                     if (aEvent.detail==2)
  113.                         onDblClick(aEvent, true);
  114.                 return;
  115.             }            
  116.         }
  117.     };
  118.        
  119.     function onClickTree(aEvent)
  120.     {                          
  121.         //TRACE("onTreeClick: id - " + aEvent.originalTarget.localName);                                         
  122.         m_eClicked = null;                          
  123.         var tbo = aEvent.originalTarget.parentNode.boxObject;                
  124.         if (!(tbo.view instanceof Ci.nsINavHistoryResultTreeViewer))
  125.             return;
  126.             
  127.         var row = { }, col = { }, obj = { };
  128.         tbo.getCellAt(aEvent.clientX, aEvent.clientY, row, col, obj);            
  129.         if (row.value == -1 || obj.value == "twisty" || tbo.view.isContainer(row.value))
  130.         {
  131.             //TRACE("onTreeClick: na/twisty/container - out");
  132.             return;
  133.         }           // check ingutter
  134.         var mouseInGutter = false;
  135.         var x = { }, y = { }, w = { }, h = { };
  136.         tbo.getCoordsForCellItem(row.value, col.value, "image", x, y, w, h);
  137.         mouseInGutter = aEvent.clientX < x.value;  
  138.         if (mouseInGutter)
  139.             return;
  140.         
  141.         var eClicked = tbo.view.nodeForTreeIndex(row.value);
  142.         if (!PlacesUtils.nodeIsURI(eClicked)) 
  143.             return;
  144.         //TRACE("onTreeClick: - Suspend Click");  
  145.         m_eClicked = eClicked;           
  146.         killEvent(aEvent);        
  147.         var aEvt = cloneEvent(aEvent, false);
  148.         var aObj = aEvent.originalTarget;
  149.         setTimeout(function(){ doClick(aObj, aEvt, true);}, _spClickDelay);               
  150.     };    
  151.  
  152.     function onClickDoc(aEvent)
  153.     {       
  154.         //TRACE("onDocClick: - in " + aEvent.target);    
  155.         m_eClicked = null;                          
  156.                     // only links... form browser.js
  157.         var target = aEvent.target;
  158.         var linkNode;
  159.                 
  160.         if (target instanceof HTMLAnchorElement ||
  161.             target instanceof HTMLAreaElement ||
  162.             target instanceof HTMLLinkElement) {
  163.             if (target.hasAttribute("href"))
  164.                 linkNode = target;
  165.  
  166.             var parent = target.parentNode;
  167.             while (parent) {
  168.                 if (parent instanceof HTMLAnchorElement ||
  169.                     parent instanceof HTMLAreaElement ||
  170.                     parent instanceof HTMLLinkElement) {
  171.                     if (parent.hasAttribute("href"))
  172.                       linkNode = parent;
  173.                 }
  174.                 parent = parent.parentNode;
  175.             }
  176.         }       
  177.         else {
  178.           linkNode = aEvent.originalTarget;
  179.           while (linkNode && !(linkNode instanceof HTMLAnchorElement))
  180.             linkNode = linkNode.parentNode;
  181.           if (linkNode && !linkNode.hasAttribute("href"))
  182.             linkNode = null;
  183.         }                          
  184.         if (!linkNode)
  185.             return;  
  186.         //TRACE("onDocClick: - Suspend Click");    
  187.         m_eClicked = linkNode;                   
  188.         killEvent(aEvent);        
  189.         var aEvt = cloneEvent(aEvent, false);  
  190.         setTimeout(function() { doClick(target, aEvt, false);}, _spClickDelay);               
  191.     }; 
  192.           
  193.     function onDblClick(aEvent, bTree)
  194.     {                
  195.         //TRACE("onDblClick: - in");                                
  196.         if (!m_eClicked)
  197.             return;                
  198.  
  199.         killEvent(aEvent);
  200.         eEvt = cloneEvent(aEvent, true);    
  201.         m_bInterClick = true;
  202.         var b = aEvent.originalTarget.dispatchEvent(eEvt);
  203.         //TRACE("onDblClick: - dispatchEvent res: " + b);
  204.         if ( bTree && gBrowser.mPrefs.getBoolPref("extensions.dblclicker.closesidebar") )
  205.             setTimeout(function(){ toggleSidebar(); }, 150);  
  206.     };
  207.     
  208.     function doMouseup(aObj, aEvt)
  209.     {   
  210.         //TRACE("doClick: - in");                                
  211.         if (!m_eClicked)// there was dclick...                          
  212.             return; 
  213.             
  214.         m_bInterClick = true;   
  215.         var windowUtils = window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
  216.         windowUtils.sendMouseEvent(aEvt.type, aEvt.clientX, aEvt.clientY, 0, 1, 0);                
  217.     };
  218.     
  219.     function doClick(aObj, aEvt, bTree)
  220.     {                   
  221.         //TRACE("doClick: - in");                                
  222.         if (!m_eClicked)// there was dclick...                          
  223.             return; 
  224.             
  225.         m_bInterClick = true;        
  226.         aObj.dispatchEvent(aEvt);         
  227.         if ( bTree && gBrowser.mPrefs.getBoolPref("extensions.dblclicker.closesidebar") )
  228.             toggleSidebar();     
  229.     };
  230.     
  231.     function cloneEvent(aEvent, ctrlKey)
  232.     {
  233.         var tmp = document.createEvent ("MouseEvents");
  234.         tmp.initMouseEvent ("click", true, true, aEvent.view, 1 /*aEvent.detail*/, aEvent.screenX, aEvent.screenY, aEvent.clientX, aEvent.clientY,
  235.             ctrlKey, aEvent.altKey, aEvent.shiftKey, aEvent.metaKey,  aEvent.button, aEvent.relatedEventTarget);
  236.         return tmp;
  237.     };
  238.     
  239.     function killEvent(aEvent)
  240.     {
  241.         aEvent.preventDefault();  
  242.         aEvent.stopPropagation();    
  243.     };
  244. };